﻿@page "/datagrid/advantage"
@using BootBlazorUI.DataGrid
@code{
    class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }

    List<User> dataSource = new List<User>();
    List<User> list;

    public int TotalCount { get; set; }
    public int Page { get; set; } = 1;
    public int Size { get; set; } = 5;
}

<PresentationPart Title="被动加载数据">
    <RunTemplate>
        <BootButton OnClick="e=>GetData()">点击加载</BootButton>
        <BootDataGrid DataSourceProvider="() => list" LoadDataAfterRender="false" @ref="grid" OnDataLoaded="e=>SetTotalCount()">
            <BootDataGridFieldColumn Field="Id" />
            <BootDataGridFieldColumn Field="Name" />
            <BootDataGridFieldColumn Field="Birthday" />
        </BootDataGrid>
        <BootPagination @bind-CurrentPage="Page" @bind-TotalCount="TotalCount" OnPageChanged="GetData"/>
    </RunTemplate>
</PresentationPart>


<PresentationPart Title="控制数据">
    <RunTemplate>
        @*<BootButton OnClick="e=>GetData()">点击加载</BootButton>*@
        <BootButton OnClick="Add">新增一行</BootButton>
        <BootButton OnClick="Remove">移除一行</BootButton>
        <BootDataGrid DataSourceProvider="()=>manualList"  RowSelectedColor="Color.Warning" LoadDataAfterRender="false" @ref="manualGrid" OnRowSelected="RememberSelectRow">
            <BootDataGridFieldColumn Field="Id" />
            <BootDataGridFieldColumn Field="Name" />
            <BootDataGridFieldColumn Field="Birthday" />
        </BootDataGrid>
    </RunTemplate>
</PresentationPart>
@code{
    BootDataGrid grid;

    BootDataGrid manualGrid;
    List<User> manualList = new List<User>();

    protected override void OnInitialized()
    {
        var random = new Random();
        for (int i = 0; i < 1000; i++)
        {
            dataSource.Add(new User
            {
                Id = (i + 1),
                Birthday = new DateTime(random.Next(1980, 1999), random.Next(3, 12), random.Next(1, 30)),
                Name = random.Next(99999).ToString().PadLeft(5, '0')
            });
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await GetData();
        }
    }

    async Task GetData()
    {
        list = dataSource.Skip((Page - 1) * Size).Take(Size).ToList();

        grid.ShowLoading();
        await Task.Delay(3000);
        await grid.LoadData();
        grid.HideLoading();
    }

    void SetTotalCount()
    {
        TotalCount = dataSource.Count;
    }

    async Task Add()
    {
        manualList.Add(new User
        {
            Id = new Random().Next(),
            Name = new Random().Next().ToString()
        });
        await manualGrid.LoadData();
        await KeepSelectRow();
    }
    async Task Remove()
    {
        manualList.RemoveAt(manualList.Count-1);
        await manualGrid.LoadData();
        await KeepSelectRow();
    }
    int selectedIndex = -1;
    User selectedItem;
    void RememberSelectRow(BootDataGridRowSelectedEventArgs e)
    {
        selectedIndex = e.Index;
        selectedItem = e.Item as User;
    }

    async Task KeepSelectRow()
    {
        if (selectedIndex > -1)
        {
            if (manualList.Contains(selectedItem))
            {
                await manualGrid.ClickRow(selectedIndex);
            }
        }
    }
}